﻿using Oliving.Edge.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Oliving.Edge.Model.Seed;
using System.Net.Http;
using System;
using System.Text.Json;

namespace Oliving.Edge.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize(Permissions.Name)]
    public class DbFirstController : ControllerBase
    {
        private readonly MyContext myContext;
        private readonly EdgeContext edgeContext;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="myContext"></param>
        public DbFirstController(MyContext myContext, EdgeContext _edgeContext)
        {
            this.myContext = myContext;
            this.edgeContext = _edgeContext;

        }

        /// <summary>
        /// 获取 整体框架 文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public bool GetFrameFiles()
        {

            return EdgeFrameSeed.EdgeCreateClass(edgeContext);

            //  return FrameSeed.CreateModels(_edgeContext)
            //      && FrameSeed.CreateIRepositorys(_edgeContext)
            //      && FrameSeed.CreateIServices(_edgeContext)
            //      && FrameSeed.CreateRepository(_edgeContext)
            //      && FrameSeed.CreateServices(_edgeContext)

            //  return FrameSeed.CreateModels(myContext)
            //      && FrameSeed.CreateIRepositorys(myContext)
            //      && FrameSeed.CreateIServices(myContext)
            //      && FrameSeed.CreateRepository(myContext)
            //      && FrameSeed.CreateServices(myContext)
            ;
        }



        [HttpGet]
        public bool InsertfunctionData()
        {
            return EdgeFrameSeed.InsertfunctionData(edgeContext);
        }

        //====================================================================================

        /// <summary>
        /// 数据库导出数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public bool EdgeGetData()
        {
            return EdgeFrameSeed.EdgeGetData(edgeContext);
        }




        #region MyRegion

        /// <summary>
        /// 获取 Model 层文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public bool GetModelFiles()
        {

            return FrameSeed.CreateModels(myContext);

        }

        /// <summary>
        /// 获取 IRepository 层文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public bool GetIRepositoryFiles()
        {
            return FrameSeed.CreateIRepositorys(myContext);
        }

        /// <summary>
        /// 获取 IService 层文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public bool GetIServiceFiles()
        {
            return FrameSeed.CreateIServices(myContext);
        }

        /// <summary>
        /// 获取 Repository 层文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public bool GetRepositoryFiles()
        {
            return FrameSeed.CreateRepository(myContext);
        }

        /// <summary>
        /// 获取 Services 层文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public bool GetServicesFiles()
        {
            return FrameSeed.CreateServices(myContext);
        }

        #endregion

 
        //[HttpGet]
        //public bool car()
        //{
        //    HttpClient httpClient = new HttpClient();

        //    #region 开通一辆车并计时
        //    object sendTimeContent = new
        //    {
        //        terminalPhoneNo = "12345678901",
        //        endTime = DateTime.Now.AddMinutes(6).ToString("yyyy-MM-dd HH:mm:ss")
        //    };
        //    var request = new HttpRequestMessage(HttpMethod.Get, "http://60.2.127.163:828/jt808api/StartOneCarByTime" + "?tokent=123456");
        //    request.Content = new StringContent(JsonSerializer.Serialize(sendTimeContent));
        //    var response = httpClient.SendAsync(request).Result;
        //    response.EnsureSuccessStatusCode();
        //    var a = response.Content.ReadAsByteArrayAsync().Result;


        //    System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
        //    //byte[] inputBytes = converter.GetBytes(inputString);
        //    string inputString = converter.GetString(a);

        //    var rr = System.Text.Encoding.UTF8.GetString(a);

        //    var value = JsonSerializer.Deserialize<JT808ResultDto<bool>>(response.Content.ReadAsByteArrayAsync().Result);
        //    response.Dispose();
        //    #endregion

        //    return true;
        //}


    }


    public class JT808ResultDto<T>
    {
        public JT808ResultDto()
        {
        }

        public string message { get; set; }

        public int code { get; set; }

        public T data { get; set; }
    }


}